package com.dayuanit.booksystem.v2.service.impl;

import com.dayuanit.booksystem.v2.dao.BookDao;
import com.dayuanit.booksystem.v2.dao.PublisherDao;
import com.dayuanit.booksystem.v2.dao.impl.BookDaoImpl;
import com.dayuanit.booksystem.v2.dao.impl.PublisherDaoImpl;
import com.dayuanit.booksystem.v2.entity.Publisher;
import com.dayuanit.booksystem.v2.service.PublisherService;
import com.dayuanit.booksystem.v2.thread.ConnectionThread;
import com.dayuanit.booksystem.v2.util.DataBaseUtil;
import com.dayuanit.booksystem.v2.util.StringUtils;

import java.sql.Connection;

public class PublisherServiceImpl implements PublisherService {

    private PublisherDao publisherDao = new PublisherDaoImpl();
    private BookDao bookDao = new BookDaoImpl();

    @Override
    public void addPublisher(String name, String address) {
        if (StringUtils.isBlank(name) || StringUtils.isBlank(address)) {
            System.out.println("请输入出版社名称或地址");
            return;
        }

        Connection connection = DataBaseUtil.getConnection();
        ConnectionThread.connThreadLocal.set(connection);

        try {
            //调用DAO层，进行添加
            publisherDao.add(name, address);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }
    }

    @Override
    public Publisher queryPublisher(String publisherName) {
        Connection connection = DataBaseUtil.getConnection();
        ConnectionThread.connThreadLocal.set(connection);

        try {
            //调用DAO层，进行添加
           return publisherDao.getPublisherByName(publisherName);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }
    }

    @Override
    public Publisher queryPublisher(long publisherId) {
        Connection connection = DataBaseUtil.getConnection();
        ConnectionThread.connThreadLocal.set(connection);

        try {
            //调用DAO层，进行添加
            return publisherDao.getPublisherById(publisherId);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }
    }

    @Override
    public void updatePublisherAddress(long publisherId, String address) {
        Connection connection = DataBaseUtil.getConnection();
        ConnectionThread.connThreadLocal.set(connection);

        try {
            //调用DAO层，进行添加
            publisherDao.updatePublisherAddress(publisherId, address);
        } finally {
            DataBaseUtil.close(connection);
            ConnectionThread.connThreadLocal.remove();
        }
    }

    @Override
    public void deletePubliser(long publisherId) {

    }

    /**
     * 由于此业务需要添加出版社 还要添加图书，要保证这两个SQL操作，同时成功，同时失败；
     * 不能有一个成功，一个失败的现象，这个时候，我们就要采用事务的特性，来解决数据不一致的问题。
     *
     *
     * 1.如何获取自增ID
     * 2.如何保证多条sql同时成功同时失败，这就需要事务支持。
     *
     * @param publisherName
     * @param publisherAddress
     * @param bookName
     * @param author
     * @param price
     */
    @Override
    public void addPublisherAndBook(String publisherName, String publisherAddress,
                                    String bookName, String author, String price) {

        //开启事务，也就是告诉数据库，以下执行的sql语句，都默认不生效
        //当程序执行 commit，则sql生效；如果执行rollback，则不生效，当什么都没发生过一样。

        //开启事务 本质就是：关闭自动提交。
        Connection connection =  DataBaseUtil.getConnection(false);

        System.out.println(">>>>>>>service:" + connection.toString());

        //放到当前线程中  为了不通过传参，在DAO中就能够获取Connection参数
        ConnectionThread.connThreadLocal.set(connection);//把Connection放到当前线程中

        try {
            // 添加出版社 自动返回数据库自增ID
            long publiserId = publisherDao.add(publisherName, publisherAddress);

//            int num = 9 / 0;//抛异常

            // 添加图书 如何获取出版社ID
            bookDao.add(bookName, author, price, publiserId);

            //提交事务
            DataBaseUtil.commit(connection);

        } catch (Exception e) {
            e.printStackTrace();
            //有异常，要做回滚操作
            //回滚事务
            DataBaseUtil.rollback(connection);
        } finally {
            //不是真正的close掉，仅仅是把Connection放回到集合中。
            DataBaseUtil.close(connection);

            //将保存在线程里面的Connection移走
            ConnectionThread.connThreadLocal.remove();
        }
    }
}
